Soru & Cevap

Implemantasyon

04.08.2017 - 11:42

Bu koddaki OnClickListenerları daha kısa  bir şekilde implement etmek istiyorum yani kodun kısalmasını isitiyorum.Bunu interface ile nasıl yapabilirim?

MainActivity.class

package com.example.tckcakar.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements OnClickListener {
private float ilkdeger=0;
    private  double sonuc=0;
    private  String islem;
    public MainActivity() {

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
final TextView txt= (TextView) findViewById(R.id.txt);
        Button c= (Button) findViewById(R.id.sil);
        Button btn0= (Button) findViewById(R.id.buton0);
        Button btn1= (Button) findViewById(R.id.buton1);
        Button btn2= (Button) findViewById(R.id.buton2);
        Button btn3= (Button) findViewById(R.id.buton3);
        Button btn4= (Button) findViewById(R.id.buton4);
        Button btn5= (Button) findViewById(R.id.buton5);
        Button btn6= (Button) findViewById(R.id.buton6);
        Button btn7= (Button) findViewById(R.id.buton7);
        Button btn8= (Button) findViewById(R.id.buton8);
        Button btn9= (Button) findViewById(R.id.buton9);
        final Button topla= (Button) findViewById(R.id.topla);
        Button çıkar= (Button) findViewById(R.id.çıkar);
        Button çarp= (Button) findViewById(R.id.çarpma);
        Button böl= (Button) findViewById(R.id.bölme);
        final Button sonuç= (Button) findViewById(R.id.Sonuç);

btn1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        txt.setText(txt.getText()+ "1");
    }
});

        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText().toString()+ "2");
            }
        });

        btn3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "3");
            }
        });

        btn4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "4");
            }
        });

        btn5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "1");
            }
        });

        btn6.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "6");
            }
        });

        btn7.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "7");
            }
        });

        btn8.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "8");
            }
        });

        btn9.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "9");
            }
        });
        btn0.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txt.setText(txt.getText()+ "0");

            }
        });

        try {c.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ilkdeger=0;
                        sonuc=0;
                txt.setText("");
            }
        });

        }
catch (Exception C){

    }

    try {topla.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
           if(!txt.getText().toString().isEmpty()){
               ilkdeger= Float.parseFloat(txt.getText().toString());
               txt.setText("");
               islem="Toplama";
           }

        }
    });

    }
catch (Exception C){

    }


        try {çıkar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!txt.getText().toString().isEmpty()){
                    ilkdeger= Float.parseFloat(txt.getText().toString());
                    txt.setText("");
                    islem="Çıkarma";
                }

            }
        });

        }
        catch (Exception C){

        }

        try {çarp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!txt.getText().toString().isEmpty()){
                    ilkdeger= Float.parseFloat(txt.getText().toString());
                    txt.setText("");
                    islem="Çarpma";
                }

            }
        });

        }
        catch (Exception C){

        }


        try {böl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!txt.getText().toString().isEmpty()){
                    ilkdeger= Float.parseFloat(txt.getText().toString());
                    txt.setText("");
                    islem="Bölme";
                }

            }
        });

        }
        catch (Exception C){

        }

        sonuç.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!txt.getText().toString().isEmpty() && !String.valueOf(ilkdeger).equals("0")){
                    if(islem.equals("Toplama")){
                        sonuc= ilkdeger + Float.parseFloat(txt.getText().toString());
            }
                    else if(islem.equals("Çıkarma")) {
                        sonuc = ilkdeger - Float.parseFloat(txt.getText().toString());
                    }

                        else if(islem.equals("Çarpma")){
                            sonuc= ilkdeger * Float.parseFloat(txt.getText().toString());
        }
                    else if(islem.equals("Bölme")) {
                        sonuc = ilkdeger / Float.parseFloat(txt.getText().toString());


                    }

                    txt.setText(String.valueOf(sonuc));
                    ilkdeger=0;
                            sonuc=0;


                }

            else{
            txt.setText("Değer giriniz");

                }

                if (R.id.sil == view.getId()
                        ) {
                    ilkdeger=0;
                    sonuc=0;
                    txt.setText("");
                }
                }

          });}

    @Override
    public void onClick(View view) {

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tckcakar.myapplication.MainActivity">

    <TextView
        android:id="@+id/txt"
        android:layout_width="167dp"
        android:layout_height="44dp"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/linear1"
        android:layout_marginTop="53dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.073"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.034" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/linear1"
        android:layout_below="@+id/txt"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="11dp">

        <Button
            android:id="@+id/buton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="1" />

        <Button
            android:id="@+id/buton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="2"
             />

        <Button
            android:id="@+id/buton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="3" />

        <Button
            android:id="@+id/sil"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="C" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/linear2"
        android:layout_alignTop="@+id/linear1"
        android:layout_alignParentStart="true"
        android:layout_marginTop="53dp">

        <Button
            android:id="@+id/buton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="4"
            />

        <Button
            android:id="@+id/buton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="5"
            />

        <Button
            android:id="@+id/buton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="6"
            />

        <Button
            android:id="@+id/topla"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="+" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/linear3"
        android:layout_alignTop="@+id/linear2"
        android:layout_alignParentStart="true"
        android:layout_marginTop="53dp">

        <Button
            android:id="@+id/buton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="7"
            />

        <Button
            android:id="@+id/buton8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="8"
            />

        <Button
            android:id="@+id/buton9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="9"
            />

        <Button
            android:id="@+id/çıkar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="-" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/linear4"
        android:layout_alignTop="@+id/linear3"

        android:layout_marginTop="53dp">

        <Button
            android:id="@+id/buton0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="0"
            />

        <Button
            android:id="@+id/bölme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="/" />

        <Button
            android:id="@+id/çarpma"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="*" />

        <Button
            android:id="@+id/Sonuç"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="=" />

    </LinearLayout>


</RelativeLayout>

 

47 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-40423-1478544775.jpg
VolkanSahin45
04.08.2017 - 01:52

.Yorum atınca kodlar baya karışmış burdan paylaşayım:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button btn1;

    Button btn2;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                //buton 1 için bir şeyler yap
                break;
            case R.id.btn2:
                //buton 2 için bir şeyler yap
                break;
        }
    }
}

picture-40423-1478544775.jpg
VolkanSahin45
04.08.2017 - 01:15

Activityyi onClickListenerdan kalıtım yaptır. Sonra onClick methodunu implente et. Butonlara da buton.setOnClickListener(this) diye atama yap. Classtaki tek onclick methodunun için de de istediğin butona göre örneğin switch case kullanarak işlemlerini yaptır.

Kardelen ÇAKAR
04.08.2017 - 01:37
yeni bir class oluşturmam lazım değil mi
Volkan Şahin
04.08.2017 - 01:51
Şu şekilde yapabilirsin. public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button btn1; Button btn2; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button)findViewById(R.id.btn1); btn2 = (Button)findViewById(R.id.btn2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: //buton 1 için bir şeyler yap break; case R.id.btn2: //buton 2 için bir şeyler yap break; } } }